import GameTool from "../../common/tool/GameTool";
import LogTool from "../../common/tool/LogTool";
import ContextComponent from "../../common/tool/context/ContextComponent";
import ContextPool from "../../common/tool/context/ContextPool";
import BaseBasicesData from "../../expand/base/BaseBasicesData";
import GameSnake from "../GameSnake";

const { ccclass, property } = cc._decorator;
@ccclass
export default class BodyCube extends ContextComponent {
    @property(cc.Label)
    public LabelNumber: cc.Label = null;
    @property(cc.Color)
    public ColorArray: Array<cc.Color> = [];


    protected mNumber: number = null;
    public set Number(data: number) {
        this.mNumber = data;
        this.refreshNumber();
        this.refershColor();
    }
    public get Number(): number {
        return this.mNumber;
    }

    protected mGameSnake: GameSnake = null;
    public init(content: ContextPool): boolean {
        super.init(content);
        this.mGameSnake = this.getContext(GameSnake);
        return true;
    }

    public reduceNumber(reduce: number): boolean {

        if (this.Number - reduce >= 0) {
            this.Number = this.Number - reduce;
            this.mGameSnake.killSnakeBody();
            return true;
        }
        else {
            this.Number = 0;
            return false;
        }
    }

    public refreshNumber(): void {
        this.LabelNumber.string = parseInt(this.Number.toString() + "") + "";
    }

    public refershColor(): void {
        let index = 0;
        index = parseInt(this.Number / 5 + "");
        if (index >= this.ColorArray.length)
            index = this.ColorArray.length;
        if (GameTool.IsSafeArrayIndex(index, this.ColorArray))
            this.node.color = this.ColorArray[index];
        else
            this.node.color = this.ColorArray[0];
    }

    public onPreSolve(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider): void {
        let tag = selfCollider.tag;
        switch (tag) {
            case 1:
                LogTool.Log("Left");
                break;
            case 2:
                LogTool.Log("Right");
                break;
            case 3:
                this.reduceNumber(1);
                if (this.Number <= 0)
                    GameTool.DestoryNode(this);
                
                break;
            default:
                break;
        }
    }
}
